package lol.clann.minecraft.springboot.api.model;

import lombok.NonNull;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

/**
 *
 * @author pyz
 * @date 2019/6/23 10:17 AM
 */
public class ClickCallback {
    /**
     * 回调的唯一标示
     */
    private final String key = UUID.randomUUID().toString();
    /**
     * null:所有玩家都可执行
     * not null:指定的玩家才能执行
     */
    private Set<String> players;
    /**
     * 回调有效期(ms)
     * 默认10分钟
     */
    private long timeout;
    /**
     * 可以点几次
     * 默认无限次
     */
    private int limit;
    /**
     * 是否异步执行回调
     */
    private boolean async;
    /**
     * 回调
     */
    @NonNull
    private Consumer<Player> callback;

    ClickCallback(Set<String> players, long timeout, boolean async, @NonNull Consumer<Player> callback) {
        this.players = players;
        this.timeout = timeout;
        this.callback = callback;
    }

    public static ClickCallbackBuilder builder() {
        return new ClickCallbackBuilder();
    }

    public boolean isAsync() {
        return async;
    }

    public String getKey() {
        return key;
    }

    public Set<String> getPlayers() {
        return players;
    }

    public long getTimeout() {
        return timeout;
    }

    public int getLimit() {
        return limit;
    }

    public Consumer<Player> getCallback() {
        return callback;
    }

    public static class ClickCallbackBuilder {
        private Set<String> players;
        private long timeout = 600000;
        private int limit = Integer.MAX_VALUE;
        private boolean async;
        private @NonNull Consumer<Player> callback;

        ClickCallbackBuilder() {
        }

        public ClickCallbackBuilder limit(int limit) {
            this.limit = limit;
            return this;
        }

        public ClickCallbackBuilder anyPlayer() {
            players = null;
            return this;
        }

        public ClickCallbackBuilder nonPlayer() {
            players = new HashSet<>();
            return this;
        }

        public ClickCallbackBuilder onlinePlayer() {
            for (Player player : Bukkit.getServer().getOnlinePlayers()) {
                player(player.getName());
            }
            return this;
        }

        public ClickCallbackBuilder player(String player) {
            if (player != null) {
                if (players == null) {
                    players = new HashSet<>();
                }
                players.add(player);
            }
            return this;
        }

        public ClickCallbackBuilder players(Iterable<String> players) {
            if (players != null) {
                for (String player : players) {
                    player(player);
                }
            }
            return this;
        }

        public ClickCallbackBuilder timeout(long timeout) {
            this.timeout = timeout;
            return this;
        }

        public ClickCallbackBuilder async(boolean async) {
            this.async = async;
            return this;
        }

        public ClickCallbackBuilder callback(@NonNull Consumer<Player> callback) {
            this.callback = callback;
            return this;
        }

        public ClickCallback build() {
            return new ClickCallback(players, timeout, async, callback);
        }

        @Override
        public String toString() {
            return "ClickCallback.ClickCallbackBuilder(players=" + this.players + ", timeout=" + this.timeout + ", callback=" + this.callback + ")";
        }
    }

}
